package com.tk.exam.service.impl;

import com.github.pagehelper.PageInfo;
import com.tk.exam.dao.ExamManageDao;
import com.tk.exam.domain.TkExamInfo;
import com.tk.exam.domain.TkExamInfoExample;
import com.tk.exam.exception.ApiException;
import com.tk.exam.mapper.TkExamInfoExtMapper;
import com.tk.exam.mapper.TkExamInfoMapper;
import com.tk.exam.service.TkExamManageService;
import com.tk.exam.utils.DateUtils;
import com.tk.exam.utils.constant.ExamStatusConstant;
import com.tk.exam.vo.exam.DelExamReq;
import com.tk.exam.vo.exam.SearchExamListRespVo;
import com.tk.exam.vo.exam.TkExamInfoExt;
import com.tk.exam.vo.exam.UpExamReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import static com.tk.exam.vo.exam.TkExamInfoExt.EXAM_STATUS_BEFORE;

@Service
public class TkExamManageServiceImpl implements TkExamManageService {
    @Autowired
    private ExamManageDao examManageDao;
    @Autowired
    private TkExamInfoExtMapper examInfoExtMapper;
    @Autowired
    private TkExamInfoExtMapper tkExamInfoExtMapper;
    @Override
    public  PageInfo<SearchExamListRespVo> getListWithPage() {
        return examManageDao.searchExamWithPage();
    }

    @Override
    public List<SearchExamListRespVo> getExamList() {
        updateExamStatus();
        return tkExamInfoExtMapper.searchExamList();
    }

    public void updateExamStatus() {
        List<SearchExamListRespVo> searchExamListRespVos = tkExamInfoExtMapper.searchExamList();
        searchExamListRespVos.forEach( searchExamListRespVo -> {
            //考试开始时间
            Date examTime = searchExamListRespVo.getExamTime();
            //考试时长
            Integer examTotalTime = searchExamListRespVo.getExamTotalTime();
            //考试允许迟到时长
            Integer maxLateTime = searchExamListRespVo.getMaxLateTime();
            //考试结束时间
            Date endTime = DateUtils.toNewTime(examTime, (examTotalTime + maxLateTime));
            //当前时间
            Date nowTime = DateUtils.now();
            Byte examStatus;
            if(nowTime.getTime() < examTime.getTime()){//考试未开始
                examStatus = ExamStatusConstant.未开始.getStatus();
            }else if((nowTime.getTime() >=  examTime.getTime()) && (nowTime.getTime() <= endTime.getTime())){//考试中
                examStatus = ExamStatusConstant.进行中.getStatus();
            }else{
                examStatus = ExamStatusConstant.已结束.getStatus();
            }

            if(!searchExamListRespVo.getExamStatus().equals(examStatus)){
                searchExamListRespVo.setExamStatus(examStatus);
                tkExamInfoExtMapper.updateByPrimaryKey(searchExamListRespVo);
            }

        });


    }

    @Override
    public void upExam(UpExamReq req) {
        TkExamInfoExt tkExamInfoExt=new TkExamInfoExt(req);
        examInfoExtMapper.updateByPrimaryKeySelective(tkExamInfoExt);
    }

    @Override
    public void addExam(UpExamReq req) {
        TkExamInfo tkExamInfo=new TkExamInfoExt().getExtAdd(req);
        examInfoExtMapper.insertSelective(tkExamInfo);
    }

    @Override
    public void delExam(List<Integer> idList) {
        updateExamStatus();
        TkExamInfoExample examInfoExample=new TkExamInfoExample();
        examInfoExample.or().andIdIn(idList);
        List<TkExamInfo> list=tkExamInfoExtMapper.selectByExample(examInfoExample);
        list.parallelStream().forEach(
                item->{
                    if(EXAM_STATUS_BEFORE!=item.getExamStatus())
                        throw ApiException.of("考试"+item.getExamName()+"已经开始,不允许删除");
                }
        );

        TkExamInfoExample delExample=new TkExamInfoExample();
        delExample.or().andIdIn(idList);
        tkExamInfoExtMapper.deleteByExample(delExample);
    }


}
